package org.example;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.parser.JSqlParser;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.util.TablesNamesFinder;
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
import net.sf.jsqlparser.util.deparser.SelectDeParser;
import net.sf.jsqlparser.util.deparser.StatementDeParser;

import java.util.ArrayList;
import java.util.List;

public class SqlTablePrefixer {

    public static void main(String[] args) {
        String query = "select [CODE],[CONTENT],[ID] from [D_XMLB_DIC]";
        query = query.replaceAll("\\[","");
        query = query.replaceAll("]","");
        System.out.println(getAllTableNames(query));

//        System.out.println(getAllTableNames(" select [CODE],[CONTENT],[ID] from [D_XMLB_DIC]"));
        System.out.println(getAllSchemaNames("select cname,dpz_num,tt_num from cqgzfpt.V_DATA_SBJTTDPZ_XQ"));
    }


    public static List<String> getAllSchemaNames(String sql) {
        sql = sql.replaceAll("\\[","");
        sql = sql.replaceAll("]","");
        List<String> tableNames = new ArrayList<String>();
        // 解析 SQL 语句
        Statement statement = null;
        try {
            statement = CCJSqlParserUtil.parse(sql);
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }

        // 检查是否为 SELECT 语句
        if (statement instanceof Select) {
            Select selectStatement = (Select) statement;
            SelectBody selectBody = selectStatement.getSelectBody();

            // 判断是否为 PlainSelect 类型
            if (selectBody instanceof PlainSelect) {
                PlainSelect plainSelect = (PlainSelect) selectBody;

                // 修改 FROM 中的表名
                FromItem fromItem = plainSelect.getFromItem();
                if (fromItem instanceof Table) {
                    Table table = (Table) fromItem;
                    tableNames.add(table.getSchemaName());
                }

                // 修改 JOIN 中的表名
                List<Join> joins = plainSelect.getJoins();
                if (joins != null) {
                    for (Join join : joins) {
                        FromItem joinItem = join.getRightItem();
                        if (joinItem instanceof Table) {
                            Table table = (Table) joinItem;
//                            table.setName(table.getName());
                            tableNames.add(table.getSchemaName());

                        }
                    }
                }
            }

            // 使用 StatementDeParser 重新生成 SQL 语句
            StringBuilder buffer = new StringBuilder();
            StatementDeParser deParser = new StatementDeParser(buffer);
            selectStatement.accept(deParser);
        }

        return tableNames;
    }


    public static List<String> getAllTableNames(String sql) {
        sql = sql.replaceAll("\\[","");
        sql = sql.replaceAll("]","");
        List<String> tableNames = new ArrayList<String>();
        // 解析 SQL 语句
        Statement statement = null;
        try {
            statement = CCJSqlParserUtil.parse(sql);
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }

        // 检查是否为 SELECT 语句
        if (statement instanceof Select) {
            Select selectStatement = (Select) statement;
            SelectBody selectBody = selectStatement.getSelectBody();

            // 判断是否为 PlainSelect 类型
            if (selectBody instanceof PlainSelect) {
                PlainSelect plainSelect = (PlainSelect) selectBody;

                // 修改 FROM 中的表名
                FromItem fromItem = plainSelect.getFromItem();
                if (fromItem instanceof Table) {
                    Table table = (Table) fromItem;
                    tableNames.add(table.getName());
                }

                // 修改 JOIN 中的表名
                List<Join> joins = plainSelect.getJoins();
                if (joins != null) {
                    for (Join join : joins) {
                        FromItem joinItem = join.getRightItem();
                        if (joinItem instanceof Table) {
                            Table table = (Table) joinItem;
//                            table.setName(table.getName());
                            tableNames.add(table.getName());

                        }
                    }
                }
            }

            // 使用 StatementDeParser 重新生成 SQL 语句
            StringBuilder buffer = new StringBuilder();
            StatementDeParser deParser = new StatementDeParser(buffer);
            selectStatement.accept(deParser);
        }

        return tableNames;
    }

    public static String addPrefixToTables(String sql, String prefix) {
        sql = sql.replaceAll("\\[","");
        sql = sql.replaceAll("]","");
        // 解析 SQL 语句
        Statement statement = null;
        try {
            statement = CCJSqlParserUtil.parse(sql);
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }

        // 检查是否为 SELECT 语句
        if (statement instanceof Select) {
            Select selectStatement = (Select) statement;
            SelectBody selectBody = selectStatement.getSelectBody();

            // 判断是否为 PlainSelect 类型
            if (selectBody instanceof PlainSelect) {
                PlainSelect plainSelect = (PlainSelect) selectBody;

                // 修改 FROM 中的表名
                FromItem fromItem = plainSelect.getFromItem();
                if (fromItem instanceof Table) {
                    Table table = (Table) fromItem;
                    table.setName(prefix + table.getName());
                }

                // 修改 JOIN 中的表名
                List<Join> joins = plainSelect.getJoins();
                if (joins != null) {
                    for (Join join : joins) {
                        FromItem joinItem = join.getRightItem();
                        if (joinItem instanceof Table) {
                            Table table = (Table) joinItem;
                            table.setName(prefix + table.getName());
                        }
                    }
                }
            }

            // 使用 StatementDeParser 重新生成 SQL 语句
            StringBuilder buffer = new StringBuilder();
            StatementDeParser deParser = new StatementDeParser(buffer);
            selectStatement.accept(deParser);
            return buffer.toString();
        }

        return sql;
    }
}
